package org.softmed.rest.editor.comps.browser;

import javax.swing.JLabel;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import org.softmed.rest.editor.EditorUtil;
import org.softmed.rest.editor.commons.SwingUtil;
import org.softmed.rest.editor.commons.URIProvider;

public class RESTServiceBrowser extends JPanel implements URIProvider {
	String restAdminURI = "admin/rest";
	PagedListPanel pagedListPanel;
	URIProvider serverURIProvider;
	ClassSelector classSelector;

	public RESTServiceBrowser(URIProvider uriProvider) throws Throwable {
		SwingUtil.setSize(this, EditorUtil.restBrowserWidth, 700);
		MigLayout layout = new MigLayout("left,insets 0");
		setLayout(layout);

		this.serverURIProvider = uriProvider;
		pagedListPanel = new PagedListPanel(this);
		classSelector = new ClassSelector(this, pagedListPanel);

		add(classSelector, "wrap 0");
		add(pagedListPanel, "wrap 0");

		pagedListPanel.getInfoBar().add(classSelector.getCreate());
		pagedListPanel.getInfoBar().add(classSelector.getRefresh());

		disableButtons();
	}

	private void disableButtons() {
		pagedListPanel.disableButtons();
		classSelector.disableButtons();
	}

	@Override
	public String getURI() {
		String uri = serverURIProvider.getURI();
		if (uri.endsWith("/"))
			return uri + restAdminURI;
		else
			return uri + "/" + restAdminURI;

	}

	// public PagedListPanel getPagedListPanel() {
	// return pagedListPanel;
	// }

	public void setSelectionListener(SelectionListener listener) {
		pagedListPanel.setSelectionListener(listener);
		classSelector.setSelectionListener(listener);
	}

	public void showApps() throws Throwable {
		try {
			disableButtons();
			classSelector.showApplications();
			classSelector.enableButtons();
			// revalidate();
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

}
